package com.huanxing.cloud.security.service;

import com.huanxing.cloud.common.core.constant.CacheConstants;
import com.huanxing.cloud.common.core.constant.SecurityConstants;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;

import javax.sql.DataSource;

/**
 * 重写Jdbc原生方法支持redis缓存
 *
 * @author lijx
 * @since 2022/2/15 15:25
 */
public class RedisClientDetailsService extends JdbcClientDetailsService {
  public RedisClientDetailsService(DataSource dataSource) {
    super(dataSource);
    super.setSelectClientDetailsSql(SecurityConstants.DEFAULT_SELECT_STATEMENT);
    super.setFindClientDetailsSql(SecurityConstants.DEFAULT_FIND_STATEMENT);
  }

  @Override
  @Cacheable(
      value = CacheConstants.CLIENT_DETAILS_KEY,
      key = "#clientId",
      unless = "#result == null")
  public ClientDetails loadClientByClientId(String clientId) {
    return super.loadClientByClientId(clientId);
  }
}
